﻿using System.Collections.Generic;
using System.Configuration;

namespace VirtualKeyboard.Service
{
    /// <summary>
    /// 服务器配置
    /// </summary>
    public class VirtualKeyboardSection : ConfigurationSection
    {
        /// <summary>
        /// 启动项
        /// </summary>
        [ConfigurationProperty("shortcutKeys", IsRequired = true)]
        public ShortcutKeyElementCollection ShortcutKeys
        {
            get
            {
                return this["shortcutKeys"] as ShortcutKeyElementCollection;
            }
        }
    }

    public class ShortcutKeyElementCollection : ConfigurationElementCollection, IEnumerable<ShortcutKeyElement>
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new ShortcutKeyElement();

        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ShortcutKeyElement)element).Name;
        }

        public new ShortcutKeyElement this[string name]
        {
            get
            {
                return (ShortcutKeyElement)BaseGet(name);
            }
            set
            {
                var element = BaseGet(name) as ShortcutKeyElement;
                if (element == null)
                {
                    BaseAdd(value);
                }
                else
                {
                    element.Name = value.Name;
                    element.ShortcutKey = value.ShortcutKey;
                }
            }

        }

        public new IEnumerator<ShortcutKeyElement> GetEnumerator()
        {
            System.Collections.IEnumerable enumerable = this;
            var enumerator = enumerable.GetEnumerator();
            while (enumerator.MoveNext())
            {
                yield return (ShortcutKeyElement) enumerator.Current;
            }
        }

        public void Add(ShortcutKeyElement shortcutKeyElement)
        {
            BaseAdd(shortcutKeyElement);
            // Add custom code here.

        }


        public void Remove(ShortcutKeyElement shortcutKeyElement)
        {
            if (BaseIndexOf(shortcutKeyElement) >= 0)
                BaseRemove(shortcutKeyElement.Name);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
        }

    }

    public class ShortcutKeyElement : ConfigurationElement
    {
        /// <summary>
        /// 动作
        /// </summary>
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get
            {
                return this["name"] as string;
            }
            set
            {
                this["name"] = value;
            }
        }

        /// <summary>
        /// 快捷键
        /// </summary>
        [ConfigurationProperty("shortcutKey", IsRequired = true)]
        public string ShortcutKey
        {
            get
            {
                return this["shortcutKey"] as string;
            }
            set
            {
                this["shortcutKey"] = value;
            }
        }
    }
}
